<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入组织名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="设备名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入红外设备名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="设备类型" prop="type">
            <el-select v-model="queryParams.type" placeholder=""
                       clearable
                       size="small"
            >
              <el-option
                v-for="dict in statusOptionsType"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="所属设备" prop="name">
            <el-input
              v-model="queryParams.zigbeeMac"
              placeholder="所属设备名称或MAC"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              v-hasPermi="['device:infrared:query']"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['device:infrared:export']"
            >导出
            </el-button>
          </el-form-item>
        </el-form>
        <el-col>
          <el-table v-loading="loading" :data="deviceList" border>
            <el-table-column label="手机号" align="center" prop="account" width="120"/>
            <el-table-column label="家庭名称" align="center" prop="homeName" :show-overflow-tooltip="true"/>
            <el-table-column label="所属设备" align="center" prop="zigbeeDeviceName" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <el-popover trigger="hover" placement="bottom">
                  <div style="width: 300px">
                    <el-descriptions :column="1">
                      <el-descriptions-item label="设备类型" :show-overflow-tooltip="true">{{
                          scope.row.zigbeeDeviceType
                        }}
                      </el-descriptions-item>
                      <el-descriptions-item label="类型名称" :show-overflow-tooltip="true">
                        {{ scope.row.zigbeeDeviceDefaultName }}
                      </el-descriptions-item>
                      <el-descriptions-item label="设备MAC" :show-overflow-tooltip="true">{{
                          scope.row.zigbeeMac
                        }}
                      </el-descriptions-item>
                    </el-descriptions>
                  </div>
                  <span slot="reference">
                          {{ scope.row.zigbeeDeviceName }}
                        </span>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column label="设备名称" align="center" prop="name" :show-overflow-tooltip="true"/>
            <el-table-column label="组织名称" align="center" prop="deptName" :show-overflow-tooltip="true"/>
            <el-table-column label="创建时间" align="center" prop="createTime" width="180" :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="100">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="handleDetailsView(scope.row)"
                  v-hasPermi="['device:infrared:detail']"
                >详情
                </el-button>

              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-col>
    </el-row>

    <!-- 详情红外设备对话框 -->
    <el-dialog :title="title" :visible.sync="details" width="700px" append-to-body>
      <el-descriptions :column=2 size="medium" labelClassName="my-label" contentClassName="my-content">
        <el-descriptions-item label="所属设备">
          {{ form.zigbeeDeviceName }}
        </el-descriptions-item>
        <el-descriptions-item label="设备名称">
          {{ form.name }}
        </el-descriptions-item>
        <el-descriptions-item label="红外类型">
          <template v-for="dict in statusOptionsType">
            <template v-if="dict.dictValue==form.type">
              <el-tag style="margin-top: -15px">{{ dict.dictLabel }}</el-tag>
            </template>
          </template>

        </el-descriptions-item>
        <el-descriptions-item label="修改时间">
          {{ form.modifyTime }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ form.createTime }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice,
  exportDevice
} from '@/api/system/infraredDevice'
import { deptTreeSelect } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'Device',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 红外设备表格数据
      deviceList: [],
      // 红外设备树选项
      deviceOptions: [],
      // 部门名称
      deptName: undefined,
      statusOptionsType: [{ dictValue: '4', dictLabel: '普通空调' }, {
        dictValue: '5',
        dictLabel: '电视'
      }, { dictValue: '6', dictLabel: '风扇' }, { dictValue: '7', dictLabel: '中央空调' }, {
        dictValue: '71',
        dictLabel: '空调管家'
      }],
      // 部门树选项
      deptOptions: undefined,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      details: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        homeId: undefined,
        deviceId: undefined,
        roomId: undefined,
        name: undefined,
        type: undefined,
        centralAcCode: undefined,
        imgUrl: undefined,
        deviceType: undefined,
        modifyTime: undefined
      },
      // 表单参数
      form: {}
      ,
      // 表单校验
      rules: {
        homeId: [
          { required: true, message: '家庭Id不能为空', trigger: 'blur' }
        ],
        deviceId: [
          { required: true, message: 'zigbee设备ID不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: ' 红外设备名称不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ]
      }
    }

  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.getTreeselect()
    this.getDictDataByType('sys_normal_disable').then(response => {
        this.statusOptions = response.data
      }
    )
    this.getDictDataByType('sys_user_sex').then(response => {
        this.sexOptions = response.data
      }
    )
    this.getConfigKey('sys.user.initPassword').then(response => {
        this.initPassword = response.msgname
      }
    )
  },
  methods: {
    /** 查询红外设备列表 */
    getList() {
      this.loading = true
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows
        this.total = response.total
        this.loading = false
      })

    },
    // 取消按钮
    cancel() {
      this.open = false
      this.details = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        homeId: undefined,
        deviceId: undefined,
        roomId: undefined,
        name: undefined,
        type: undefined,
        centralAcCode: undefined,
        imgUrl: undefined,
        deviceType: undefined,
        createTime: undefined,
        modifyTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加红外设备'
    },
    /** 详情按钮操作 */
    handleDetailsView(row) {
      this.reset()
      getDevice(row.id).then(response => {
        this.form = response.data
        this.details = true
        this.title = row.name + '详情'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改红外设备'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateDevice(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                }
              })
            } else {
              addDevice(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                }
              })
            }
          }
        }
      )
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除红外设备编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delDevice(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportDevice(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      deptTreeSelect().then(response => {
          this.deptOptions = response.data
        }
      )
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.selectedOrgCode = data.id
      this.queryParams.deptId = data.id
      this.getList()
    }
  }
}
</script>
